package mwa.cs.news.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import mwa.cs.dao.NewsDAO;
import mwa.cs.database.Database;
import mwa.cs.util.JDate;

/**
 * Servlet implementation class NewsSrvl
 */
public class NewsSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private Database db; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsSrvl() {
        super();
        
    }
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	String vaProcessType = request.getParameter("process_type");
	
	if(vaProcessType.equals("getNewsTypeS")){
		getNewsTypeS(request,response);
	}else if(vaProcessType.equals("getDeatailTypeS")){
		getDeatailTypeS(request,response);
	}else if(vaProcessType.equals("getnewsTypeP")){
		getnewsTypeP(request,response);
	}else if (vaProcessType.equals("getDeatailTypeP")){
		getDeatailTypeP(request,response);
	}
}

public void getDeatailTypeP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	request.setCharacterEncoding("UTF-8");
	response.setCharacterEncoding("UTF-8"); 
	response.setContentType("text/JSON");	
	this.db = new Database();
	int  vnSiteId;
	vnSiteId = 0;
	String vaTopic = request.getParameter("topic");
	NewsDAO voNewsDAO = new NewsDAO(db);
	Map<String, Object> vomap = voNewsDAO.findDetailByTopicTypeP(vnSiteId, vaTopic, JDate.getDate());
	JSONObject voJsonObject = new JSONObject();
	voJsonObject.put("datails",vomap.get("DETAIL").toString());
	response.getWriter().print(voJsonObject.toJSONString());
	this.db.close();
}


public void getnewsTypeP(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");	
		this.db = new Database();
		int  vnSiteId;
		vnSiteId = 0;
		String vaDate = JDate.getDate();
		String[] vaTopic = null, vaDetail = null , vaUrl = null;
		NewsDAO voNewsDAO = new NewsDAO(db);
		List<Map<String, Object>> voResult = voNewsDAO.newsTypeP(vnSiteId,vaDate);
		vaTopic = new String[voResult.size()]; 
		vaDetail = new String[voResult.size()];
		vaUrl = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);	
			vaTopic[i] = (String) voObject.get("TOPIC"); 
			vaDetail[i] = (String) voObject.get("DETAIL");	
			vaUrl[i] = (String) voObject.get("IMAGE_URL");
		}
		
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaTopic != null) {
			for (int i = 0; i < vaTopic.length; i ++) {
				voList.add(vaTopic[i]);
			}
		}
		voObject.put("topic", voList);
		
		
		JSONArray voList2 = new JSONArray();
		JSONArray voList2_5 = new JSONArray();
		 String[] vaDetailShow = null;
		if (vaDetail != null) {
			for (int i = 0; i < vaDetail.length; i ++) {
				String vaDetailArray = vaDetail[i];
				if(vaDetailArray.length()>=180){}
				vaDetailShow = vaDetailArray.split("(?<=\\G.{180})");
				for(int d=0; d<vaDetailShow.length; d++){
					String detail_split = vaDetailShow[0]+"...";
					if(d==0){
						voList2.add(detail_split);
						voObject.put("detail", voList2);
					}
				}
			}
		}
		
		JSONArray voList3 = new JSONArray();
		if (vaUrl != null) {
			for (int i = 0; i < vaUrl.length; i ++) {
				voList3.add(vaUrl[i]);
			}
		}
		voObject.put("url",voList3);
		voOut.print(voObject.toJSONString());
		this.db.close();
		
		
		
		
}
	public void getDeatailTypeS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");	
		this.db = new Database();
		int  vnSiteId;
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		String vaTopic = request.getParameter("topic");
		NewsDAO voNewsDAO = new NewsDAO(db);
		Map<String, Object> vomap = voNewsDAO.findDetailByTopic(vnSiteId, vaTopic, JDate.getDate());
		JSONObject voJsonObject = new JSONObject();
		voJsonObject.put("datails",vomap.get("DETAIL").toString());
		response.getWriter().print(voJsonObject.toJSONString());
		this.db.close();
		     
	
}
	public void getNewsTypeS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8"); 
		response.setContentType("text/JSON");	
		this.db = new Database();
		int  vnSiteId;
		vnSiteId = Integer.parseInt(request.getSession().getAttribute("site_id").toString());
		String vaDate = JDate.getDate();
		String[] vaTopic = null, vaDetail = null , vaUrl = null;
		NewsDAO voNewsDAO = new NewsDAO(db);
		List<Map<String, Object>> voResult = voNewsDAO.newsTypeS(vnSiteId,vaDate);
		vaTopic = new String[voResult.size()]; 
		vaDetail = new String[voResult.size()];
		vaUrl = new String[voResult.size()];
		for (int i = 0; i < voResult.size(); i++) {
			Map<String, Object> voObject = voResult.get(i);	
			vaTopic[i] = (String) voObject.get("TOPIC"); 
			vaDetail[i] = (String) voObject.get("DETAIL");	
			vaUrl[i] = (String) voObject.get("IMAGE_URL");
		}
					
		JSONObject voObject = new JSONObject();
		JSONArray voList = new JSONArray();
		PrintWriter voOut = response.getWriter();
		if (vaTopic != null) {
			for (int i = 0; i < vaTopic.length; i ++) {
				voList.add(vaTopic[i]);
			}
		}
		voObject.put("topic", voList);
		
					
		JSONArray voList2 = new JSONArray();
		JSONArray voList2_5 = new JSONArray();
		String[] vaDetailShow = null;
		if (vaDetail != null) {
			for (int i = 0; i < vaDetail.length; i ++) {
				String vaDetailArray = vaDetail[i];
				if(vaDetailArray.length()>=180){}
				vaDetailShow = vaDetailArray.split("(?<=\\G.{180})");
				for(int d=0; d<vaDetailShow.length; d++){
					String vaDetailplit = vaDetailShow[0]+"...";
					if(d==0){
						voList2.add(vaDetailplit);
						voObject.put("detail", voList2);
					}
				}
			}
		}
					
		JSONArray voList3 = new JSONArray();
		if (vaUrl != null) {
			for (int i = 0; i < vaUrl.length; i ++) {
				voList3.add(vaUrl[i]);
			}
		}
		voObject.put("url",voList3);
		voOut.print(voObject.toJSONString());
		this.db.close();	
}


	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}
		
		

}
